<template>
  <div class="MidTransform" id="MidTransform">
    <el-form id="mid_form" ref="form" :model="form_input" label-width="80px" @submit.native.prevent >
      <el-input id="mid" v-model="form_input.input" @keyup.enter.native="press_enter()"></el-input>
      <el-form-item>
      </el-form-item>
      <el-button type="primary" @click.prevent="trsMID()">ID转换</el-button>
      <el-button type="primary" @click.prevent="trsName()">名字转换</el-button>
      <el-button type="primary" @click.prevent="hotPlay()">正在热映</el-button>
      <el-button type="primary" @click.prevent="transformCelebrity()">影人信息</el-button>
    </el-form>

    <el-form ref="form" :model="form_output" label-width="80px" @submit.native.prevent>
      <el-form-item>
      </el-form-item>
      <el-input type='textarea' rows="8" cols="80" v-model="form_output.output"></el-input>
    </el-form>
  </div>
</template>

<script>
  import $ from 'jquery'
  export default {
    name: 'MidTransform',
    data() {
      return {
        base_url: 'https://maoyan.com/',
        filme_url: 'https://maoyan.com/films/',
        filme_search_url: 'https://maoyan.com/query?type=0&kw=',
        celebrity_search_url: 'https://maoyan.com/query?type=1&kw=',
        celebrity_id_url: 'https://maoyan.com/films/celebrity/',
        mname_search_url: 'https://i.maoyan.com/apollo/ajax/search?cityId=1&stype=-1&kw=',
        form_input: {
          input: ''
        },
        form_output: {
          output: ''
        },
        error_message: '请输入有效信息!!!'
      }
    },
    methods: {
      trsMID() {
        var input = this.form_input.input
        if (input != '') {
          var url = this.filme_url + input;
          this.$axios.get(url).then(response => {
            var array = new Array('********************************************************')
            var el = $(response.data);
            var name = $('h1.name', el).html()
            if (name == null || name.length == 0) {
              array.push('没找到你需要的资源,根据影片名称长度判断.')
            } else {
              array.push(name)
              var ename = $('div.ename.ellipsis', el).html()
              array.push(ename)
              $('li.ellipsis', el).each(function() {
                var hstr = $(this).html().replace(/\s/g, '')
                if(hstr.indexOf('<a')>=0){
                  var aaa = new Array()
                  $('a',$(this)).each(function() {
                    var astr = $(this).html().replace(/\s/g, '')
                    aaa.push(astr)
                  })
                  array.push(aaa.join(','))
                }else{
                  array.push($(this).html().replace(/\s/g, ''))
                }
               
              })
            }
            array.push('********************************************************')

            var info = array.join('\n');
            if (info[0] == '' || info[0] == null) {
              info = '没有查到该剧:' + url;
            }
            this.form_output.output = info
          });
        } else {
          this.form_output.output = this.error_message
        }
      },
      trsName() {
        if (this.form_input.input != '') {
          var html;
          var url = this.mname_search_url + this.form_input.input;
          console.log(url)
          this.$axios.get(url).then(result => {
            let movie_list = result.data['movies']['list']
            var resultArray = new Array();
            movie_list.forEach(iter =>{
                var array = new Array();
                array.push("ID:" + iter['id'])
                array.push("名称:" + iter['nm'])
                array.push(iter['cat'])
                array.push(iter['pubDesc'])
                resultArray.push(array.join("\n"))
            })
              // resultArray = resultArray.slice(0, 3);
              // resultArray.push('搜索长度>3,请前往 http://www.maoyan.com 进行搜索.')
              // resultArray.push('很抱歉，没找到相关的影视剧!')
            this.form_output.output = resultArray.join('\n**************************************************************\n')
          });
        } else {
          this.form_output.output = this.error_message
        }
      },
      hotPlay() {
        var html;
        this.$axios.get(this.base_url).then(response => {
          var el = $(response.data);
          var resultArray = new Array();
          var nowMovieList = new Array()
          var preMovieList = new Array()
          var now = $('dl.movie-list', el)[0]
          var pre = $('dl.movie-list', el)[1]
          var nowList = $(now).find('div.movie-item')
          nowList.each(function() {
            var id = $(this).find('a[data-act=playingMovie-click]').attr('href')
            var name = $(this).find('div.movie-title').html()
            id = id.substr(id.indexOf('/', 1) + 1);
            nowMovieList.push(id + '\t' + name)
          });
          resultArray.push(nowMovieList.join('\n'))

          var preList = $(pre).find('div.movie-item')
          preList.each(function() {
            var id = $(this).find('a[data-act=upcomingMovie-click]').attr('href')
            var playDate = $(this).next().html()
            var name = $(this).find('div.movie-title').html()
            id = id.substr(id.indexOf('/', 1) + 1);
            preMovieList.push(id + '\t' + playDate + '\t' + name)
          });
          resultArray.push(preMovieList.join('\n'))

          this.form_output.output = resultArray.join('\n**************************************************************\n')
        });
      },
      press_enter(){
        var input = this.form_input.input
        if (input != '') {
          if(/^\d+$/.test(input)){
            this.trsMID()
          }else{
            this.trsName()
          }
        }
      },
      getCelebirtyById(){
        var input = this.form_input.input
        if(input.trim() == ''){
          this.form_output.output = this.error_message
          return
        }
        var url = this.celebrity_id_url + input;
        // url = '/maoyan/films/celebrity/'+input
        this.$axios.get(url).then(response =>{
            let el = $(response.data);
            var array = new Array();
            var name = $('p.china-name,cele-name', el).html()
            if(name == null || name.trim().length==0 || typeof(name) == 'undefind'){
              this.form_output.output='很抱歉,没找到对应该ID('+input+')的影人'
            }else{
              var ename = $('p.eng-name,cele-name', el).html()
              if(ename != null && typeof(ename) != 'undefind'){
                array.push(name +='('+ename+')')
              }else{
                array.push(name)
              }
              let profession = $('p.property', el).find('span.profession').html()
              if(profession != null && typeof(profession) != 'undefind'){
                array.push('职业:'+ profession)
              }
              let birthday = $('p.property', el).find('span.birthday').html()
              if(birthday != null && typeof(birthday) != 'undefind'){
                array.push('生日:'+birthday)
              }
              let height = $('p.property', el).find('span.height').html()
              if(height != null && typeof(birthday) != 'height'){
                array.push('身高:'+height)
              }
              var works = new Array();
              $('div.masterpiece', el).find('li.master-item').each(function(){
                works.push('  '+$(this).find('p.movie-name').html())
              })
              if(works.length>0){
                array.push('代表作:')
                array=  array.concat(works)
              }
              this.form_output.output=array.join('\n')
            }
        });
      },
      getCelebirtyByName(){
        var input = this.form_input.input
        if (input != '') {
          var html;
          var url = this.celebrity_search_url + input;
          // url = '/maoyan/query?type=1&kw=' + input;
          this.$axios.get(url).then(result => {
            var el = $(result.data);
            var resultArray = new Array();
            if ($('ul.celebrity-list', el).length > 0) {
              var ddList = $('ul.celebrity-list', el).children('li');
              var subBoolean = false
              if(ddList.length>5){
                ddList = ddList.slice(0, 5);
                subBoolean=true;
              }
              ddList.each(function() {
                var array = new Array();
                var a = $(this).find('div.item-title').find('a');
                var name = a.html();
                var id = a.attr('href')
                var idsplit = id.split('/')
                id = idsplit[idsplit.length-1]
                var subtitle = $(this).find('div.item-subtitle').html()
                var pub = $(this).find('div.absolute-info').find('div.item-detail.item-detail-two-line.line-clamp').html()
                if (id != null && id.length > 0) {
                  array.push("ID:" + id)
                }
                if (name != null && name.length > 0) {
                  name="名字:" + name
                }
                if (subtitle != null && subtitle.length > 0) {
                  name+='('+subtitle+')'
                }
                array.push(name)
                if (pub != null && pub.length > 0) {
                  array.push(pub)
                }
                resultArray.push(array.join("\n"))
              })
              if (subBoolean) {
                resultArray.push('搜索长度>5,请前往 http://www.maoyan.com 进行搜索.')
              }
            } else {
              resultArray.push('很抱歉，没找到相关的影人信息')
            }
            this.form_output.output = resultArray.join('\n**************************************************************\n')
          });
        } else {
          this.form_output.output = this.error_message
        }
      },
      transformCelebrity(){
        var input = this.form_input.input
        if (input != '') {
          if(/^\d+$/.test(input.trim())){
            this.getCelebirtyById()
          }else{
            this.getCelebirtyByName()
          }
        }
      }
    },
    created() {
      this.hotPlay()
    }
  }
</script>

<style scoped>

</style>
